// pages/home/home.js
var start_clientX;
var end_clientX;
const app = getApp()
const util = require("../../utils/util.js")
var isShowLoading = false;
var isShowToast = false;

Page({

  data: {
    windowWidth:wx.getSystemInfoSync().windowWidth,
    photoUrl:'../../image/icon/PhotoUrl.png',
    modalHidden:true,
    index:0,
    id:0,
    empno:0,
    addtell: {
      addtellHidden: true, //弹出框显示/隐藏,
    }, 
    addtell1: {
      addtellHidden: true, //弹出框显示/隐藏,
    }, 
    addtell2: {
      addtellHidden: true, //弹出框显示/隐藏,
    }
  },
  
  setPhotoInfo:function(){
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType:['original','compressed'],
      sourceType:['album','camera'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        that.setData({
          photoUrl:tempFilePaths
        })
      },
    })
  },
  kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    // 使用id获取打开的子列表
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list: list
    });
  },
  // 滑动开始
  touchstart: function (e) {
    start_clientX = e.changedTouches[0].clientX
  },
  // 滑动结束
  touchend: function (e) {
    end_clientX = e.changedTouches[0].clientX;
    if (end_clientX - start_clientX > 120) {
      this.setData({
        display: "block",
        translate: 'transform: translateX(' + this.data.windowWidth * 0.7 + 'px);'
      })
    } else if (start_clientX - end_clientX > 20) {
      this.setData({
        display: "none",
        translate: ''
      })
    }
  },
  // 遮拦
  hideview: function () {
    this.setData({
      display: "none",
      translate: '',
    })
  },

  getInfomation:function(uid,_this){
    var that = _this
    var id = uid;
    wx.request({
      url: 'http://192.168.1.6:8080/WeChatJSON//userInfo',
      data: {
        id: id
      }, method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var dname = res.data.dname
        console.log(res)
        if (res.data.deptno === 0) {
          dname = "无"
        }
        that.setData({
          name: res.data.ename,
          dname: dname,
          empno: res.data.empno,
          job: res.data.job,
          level: res.data.level,
          deptno: res.data.deptno,
        })
        if (res.data.level == 'T3') {
          wx.request({
            url: 'http://192.168.1.6:8080/WeChatJSON//userInfoByDeptno',
            data: {
              deptno: res.data.deptno
            }, method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res)
              var salarylist = res.data.salarys;
              var vacationlist = res.data.vacations;
              var changelist = res.data.changes;
              var options = []; var options1 = [];  //处理假期
              //处理换岗和薪金
              var options2 = []; var options3 = []; var options4 = [];
              var info = []; var info1 = []; var info2 = []; var info3 = []; var info4 = [];
              for (var i = 0; i < vacationlist.length; i++) {
                if (vacationlist[i].flag == 0) {
                  options.push(vacationlist[i].ename + "-假期申请")
                  info.push({
                    vacationlist: vacationlist[i],
                    type: 'vacationlist'
                  })
                } else {
                  options1.push(vacationlist[i].ename + "-假期申请")
                  info1.push({
                    vacationlist: vacationlist[i],
                    type: 'vacationlist'
                  })
                }
              }
              for (var i = 0; i < salarylist.length; i++) {
                if (salarylist[i].flag == 0) {
                  options2.push(salarylist[i].ename + "-薪资变动")
                  info2.push({
                    salarylist: salarylist[i],
                    type: 'salarylist'
                  })
                } else if (salarylist[i].flag == 1) {
                  options3.push(salarylist[i].ename + "-薪资变动")
                  info3.push({
                    salarylist: salarylist[i],
                    type: 'salarylist'
                  })
                } else {
                  options4.push(salarylist[i].ename + "-薪资变动")
                  info4.push({
                    salarylist: salarylist[i],
                    type: 'salarylist'
                  })
                }
              }
              for (var i = 0; i < changelist.length; i++) {
                if (changelist[i].maflag == 0) {
                  options.push(changelist[i].ename + "-岗位调动")
                  info.push({
                    changelist: changelist[i],
                    type: 'changelist'
                  })
                } else {
                  if (changelist[i].maflag == -1){
                    options1.push(changelist[i].ename + "-岗位调动")
                    info1.push({
                      changelist: changelist[i],
                      type: 'changelist'
                    })
                  } else if (changelist[i].peflag == 0) {
                    options2.push(changelist[i].ename + "-岗位调动")
                    info2.push({
                      changelist: changelist[i],
                      type: 'changelist'
                    })
                  } else if (changelist[i].peflag == 1) {
                    options3.push(changelist[i].ename + "-岗位调动")
                    info3.push({
                      changelist: changelist[i],
                      type: 'changelist'
                    })
                  } else {
                    options4.push(changelist[i].ename + "-岗位调动")
                    info4.push({
                      changelist: changelist[i],
                      type: 'changelist'
                    })
                  }
                }
              }
              that.setData({
                list: [
                  {
                    id: 0,
                    name: '待处理',
                    open: false,
                    options: options,
                    msg: '处理',
                    info: info,
                    isNoShowCancel:false
                  },
                  {
                    id: 1,
                    name: '已处理',
                    open: false,
                    options: options1,
                    msg: '查看',
                    info: info1,
                    isNoShowCancel: true
                  },
                  {
                    id: 2,
                    name: '待审核',
                    open: false,
                    options: options2,
                    msg: '查看',
                    info: info2, 
                    isNoShowCancel: true
                  },
                  {
                    id: 3,
                    name: '审核通过',
                    open: false,
                    options: options3,
                    msg: '查看',
                    info: info3,
                    isNoShowCancel: true
                  },
                  {
                    id: 4,
                    name: '审核不通过',
                    open: false,
                    options: options4,
                    msg: '查看',
                    info: info4,
                    isNoShowCancel: true
                  },
                ],
                salarys: salarylist,
                vacations: vacationlist,
                changes: changelist
              })
            }
          })
        } else {
          var salarylist = res.data.salarys;
          var vacationlist = res.data.vacations;
          var changelist = res.data.changes;
          var options = []; var options1 = []; var options2 = [];
          var info = []; var info1 = []; var info2 = [];
          for (var i = 0; i < changelist.length; i++) {
            if (changelist[i].maflag == 1 && changelist[i].peflag == 1) {
              options1.push(changelist[i].ename + "-岗位调动")
              info1.push({
                changelist: changelist[i],
                type: 'changelist'
              })
            } else if (changelist[i].maflag == 0 && changelist[i].peflag == 0) {
              options.push(changelist[i].ename + "-岗位调动")
              info.push({
                changelist: changelist[i],
                type: 'changelist'
              })
            } else if (changelist[i].maflag == 1 && changelist[i].peflag == 0) {
              options.push(changelist[i].ename + "-岗位调动")
              info.push({
                changelist: changelist[i],
                type: 'changelist'
              })
            } else {
              options2.push(changelist[i].ename + "-岗位调动")
              info2.push({
                changelist: changelist[i],
                type: 'changelist'
              })
            }
          }
          for (var i = 0; i < salarylist.length; i++) {
            if (salarylist[i].flag == 0) {
              options.push(salarylist[i].ename + "-薪资变动")
              info.push({
                salarylist: salarylist[i],
                type: 'salarylist'
              })
            } else if (salarylist[i].flag == 1) {
              options1.push(salarylist[i].ename + "-薪资变动")
              info1.push({
                salarylist: salarylist[i],
                type: 'salarylist'
              })
            } else {
              options2.push(salarylist[i].ename + "-薪资变动")
              info2.push({
                salarylist: salarylist[i],
                type: 'salarylist'
              })
            }
          }
          for (var i = 0; i < vacationlist.length; i++) {
            if (vacationlist[i].flag == 0) {
              options.push(vacationlist[i].ename + "-假期申请")
              info.push({
                vacationlist: vacationlist[i],
                type: 'vacationlist'
              })
            } else if (vacationlist[i].flag == 1) {
              options1.push(vacationlist[i].ename + "-假期申请")
              info1.push({
                vacationlist: vacationlist[i],
                type: 'vacationlist'
              })
            } else {
              options2.push(vacationlist[i].ename + "-假期申请")
              info2.push({
                vacationlist: vacationlist[i],
                type: 'vacationlist'
              })
            }
          }
          that.setData({
            list: [
              {
                id: 0,
                name: '正在审核',
                open: false,
                options: options,
                msg: '查看',
                info: info,
                isNoShowCancel:true
              },
              {
                id: 1,
                name: '审核通过',
                open: false,
                options: options1,
                msg: '查看',
                info: info1,
                isNoShowCancel: true
              },
              {
                id: 2,
                name: '审核不通过',
                open: false,
                options: options2,
                msg: '查看',
                info: info2,
                isNoShowCancel: true
              }
            ],
            salarys: salarylist,
            vacations: vacationlist,
            changes: changelist
          })
        }
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight
        })
      }
    })
    this.getInfomation(options.uid, this)
  },
  bindViewTap:function(event){
    console.log(event.currentTarget.dataset.index)
    this.setData({
      modalHidden:!this.data.modalHidden,
      index: event.currentTarget.dataset.index,
      id: event.currentTarget.dataset.id
    })
    console.log(this.data.list[this.data.id])
    var isNoShowCancel = this.data.list[this.data.id].isNoShowCancel
    console.log("isNoShowCancel = "+isNoShowCancel)
    if (isNoShowCancel == true){
      this.setData({
        isNoShowCancel: isNoShowCancel,
        text:'确定'
      })
    }else {
      this.setData({
        isNoShowCancel: isNoShowCancel,
        text: '通过'
      })
    }
    if (this.data.list[this.data.id].info[this.data.index].type == 'vacationlist'){
      console.log("我进来了")
      this.setData({
        addtell: {
          empno: this.data.list[this.data.id].info[this.data.index].vacationlist.empno,
          ename: this.data.list[this.data.id].info[this.data.index].vacationlist.ename,
          startdate: this.data.list[this.data.id].info[this.data.index].vacationlist.startdate,
          enddate: this.data.list[this.data.id].info[this.data.index].vacationlist.enddate,
          reason: this.data.list[this.data.id].info[this.data.index].vacationlist.reason,
          addtellHidden: false,
        }
      })
    } else if (this.data.list[this.data.id].info[this.data.index].type == 'changelist'){
      console.log("我进来了")
      this.setData({
        addtell1: {
          empno: this.data.list[this.data.id].info[this.data.index].changelist.empno,
          ename: this.data.list[this.data.id].info[this.data.index].changelist.ename,
          ordeptno: this.data.list[this.data.id].info[this.data.index].changelist.ordeptno,
          orjob: this.data.list[this.data.id].info[this.data.index].changelist.orjob,
          newdeptno: this.data.list[this.data.id].info[this.data.index].changelist.newdeptno,
          newjob: this.data.list[this.data.id].info[this.data.index].changelist.newjob,
          reason: this.data.list[this.data.id].info[this.data.index].changelist.reason,
          addtellHidden: false,
        }
      })
    } else if (this.data.list[this.data.id].info[this.data.index].type == 'salarylist') {
      console.log("我进来了")
      this.setData({
        addtell2: {
          empno: this.data.list[this.data.id].info[this.data.index].salarylist.empno,
          ename: this.data.list[this.data.id].info[this.data.index].salarylist.ename,
          orsal: this.data.list[this.data.id].info[this.data.index].salarylist.orsal,
          newsal: this.data.list[this.data.id].info[this.data.index].salarylist.newsal,
          reason: this.data.list[this.data.id].info[this.data.index].salarylist.reason,
          addtellHidden: false,
        }
      })
    }
  },

  modalBindaconfirm:function(){
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  },
  modalBindcancel: function () {
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  },
  //通过按钮
  modalConfirm: function () {
    var type = this.data.list[this.data.id].info[this.data.index].type;
    var msg = this.data.list[this.data.id].msg;
    var that = this;
    if (type == 'vacationlist' && msg == '处理') {
      var info = this.data.list[this.data.id].info[this.data.index].vacationlist;
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateVacation',
        data:{
          id:info.id,
          flag:1
        },
        method:'GET',
        header:{
          'content-type': 'application/json'
        },
        success:function(res){
          console.log(res)
          if (that.data.empno != 0) {
            that.getInfomation(that.data.empno, that)
          }
          wx.showToast({
            title: '已通过',
            icon: 'success'
          })
        }
      })
    } else if (type == 'changelist' && msg == '处理') {
      var info = this.data.list[this.data.id].info[this.data.index].changelist;
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateChange',
        data: {
          id: info.id,
          flag: 1
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (that.data.empno != 0) {
            that.getInfomation(that.data.empno, that)
          }
          wx.showToast({
            title: '已通过',
            icon: 'success'
          })
        }
      })
    } else if (type == 'salarylist' && msg == '处理') {
      var info = this.data.list[this.data.id].info[this.data.index].salarylist;
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateSalary',
        data: {
          id: info.id,
          flag: 1
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (that.data.empno != 0) {
            that.getInfomation(that.data.empno,that)
          }
          wx.showToast({
            title: '已通过',
            icon: 'success'
          })
        }
      })
    }
    console.log(this.data.empno)
    
    //弹出框确认操作
    this.setData({
      addtell: {
        addtellHidden: true,
      },
      addtell1: {
        addtellHidden: true,
      },
      addtell2: {
        addtellHidden: true,
      },
    });
  },

  //驳回按钮
  modalCancel: function () {
    var type = this.data.list[this.data.id].info[this.data.index].type;
    var msg = this.data.list[this.data.id].msg
    var that = this;
    if (type == 'vacationlist' && msg == '处理') {
      var info = this.data.list[this.data.id].info[this.data.index].vacationlist;
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateVacation',
        data: {
          id: info.id,
          flag: -1
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (that.data.empno != 0) {
            that.getInfomation(that.data.empno, that)
          }
          wx.showToast({
            title: '已驳回',
            icon: 'success'
          })
        }
      })
    } else if (type == 'changelist' && msg == '处理') {
      var info = this.data.list[this.data.id].info[this.data.index].changelist;
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateChange',
        data: {
          id: info.id,
          flag: -1
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (that.data.empno != 0) {
            that.getInfomation(that.data.empno, that)
          }
          wx.showToast({
            title: '已驳回',
            icon: 'success'
          })
        }
      })
    } else if (type == 'salarylist' && msg == '处理') {
      var info = this.data.list[this.data.id].info[this.data.index].salarylist;
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateSalary',
        data: {
          id: info.id,
          flag: -1
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (that.data.empno != 0) {
            that.getInfomation(that.data.empno, that)
          }
          wx.showToast({
            title: '已驳回',
            icon: 'success'
          })
        }
      })
    }
    console.log(this.data.empno)
    
    //弹出框取消操作
    this.setData({
      addtell: {
        addtellHidden: true,
      },
      addtell1: {
        addtellHidden: true,
      },
      addtell2: {
        addtellHidden: true,
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      display: "none",
      translate: ''
    })
    if (this.data.empno != 0){
      this.getInfomation(this.data.empno, this)
    }
  },

})